package com.wanmait.community.controller;

import com.github.pagehelper.PageInfo;
import com.wanmait.community.aop.OperationLogInfo;
import com.wanmait.community.entity.Notice;
import com.wanmait.community.service.NoticeService;
import com.wanmait.community.util.AjaxResult;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("Notice")
public class NoticeController {
    @Resource
    private NoticeService noticeService;
    @OperationLogInfo("查询所有公告信息")
    @PostMapping("list")
    public AjaxResult list(@RequestBody Notice notice,Integer pageNum) {
        Integer pageSize = 5;
        PageInfo<Notice> pageInfo = noticeService.selectNoticeList(notice,pageNum,pageSize);
//        System.out.println(pageInfo.getList());
        return AjaxResult.success().put("pageInfo", pageInfo);
    }
    @OperationLogInfo("在线编辑器配置")
    @PostMapping("uploadSever")
    public Map<String, Object> uploadSever(MultipartFile file, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        try{
            String path = ResourceUtils.getURL("classpath:").getPath()+"/static/uploadImages/";
            //唯一文件名
            String ext = FilenameUtils.getExtension(file.getOriginalFilename());
            String fileName = UUID.randomUUID().toString().replace("-", "") + "." + ext;
            //保存
            file.transferTo(new File(path+File.separator+fileName));
            map.put("errno", 0);
            Map<String,String> data = new HashMap<>();
            String basePath = request.getRequestURL().toString().replace(request.getRequestURI(),"");
            data.put("url", basePath+"/static/uploadImages/"+fileName);
            map.put("data", data);

        }catch(IOException e){
            map.put("errno", 1);
            map.put("message", "上传失败");
        }
        return map;
    }
    @Value("${spring.servlet.multipart.max-file-size}")
    private String maxFileSize;
    @ExceptionHandler(value = MaxUploadSizeExceededException.class)
    public Map handleMaxUploadSizeExceededException(MaxUploadSizeExceededException exception) {
        Map<String,Object> map = new HashMap<>();
        map.put("errno", 1);
        map.put("message", "文件大小超出，最大允许："+maxFileSize);
        return map;
    }
    @OperationLogInfo("添加小区公告")
    @PostMapping("add")
    public AjaxResult add(@RequestBody Notice notice) {
        noticeService.insertNotice(notice);
        return AjaxResult.success();
    }

    @OperationLogInfo("根据id查询数据")
    @GetMapping("selectByid")
    public AjaxResult selectById(@RequestParam Integer id) {
        Notice notice =  noticeService.selectNoticeById(id);
        return AjaxResult.success().put("notice", notice);

    }

    @OperationLogInfo("逻辑删除小区公告")
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        noticeService.updateNotice(id);
        return AjaxResult.success("删除成功");
    }

    @OperationLogInfo("根据id查询数据")
    @GetMapping("{id}")
    public AjaxResult get(@PathVariable Integer id) {
        Notice notice = noticeService.selectNoticeById(id);
        return AjaxResult.success().put("notice", notice);
    }

    @OperationLogInfo("修改小区公告")
    @PostMapping("update")
    public AjaxResult update(@RequestBody Notice notice) {
        noticeService.updateNoticeby(notice);
        return AjaxResult.success("修改成功");
    }

}
